package com.linkme2night.view.interactwithvenue;

import java.sql.Timestamp;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.linkme2night.R;
import com.linkme2night.adapters.IwvCommentsListAdapter;
import com.linkme2night.controller.InteractWithVenueController;
import com.linkme2night.model.Comment;
import com.linkme2night.model.Rate;
import com.linkme2night.view.CustomMenuActivity;

public class InteractWithVenueActivity extends CustomMenuActivity {

	private IwvCommentsListAdapter commentsListAdapter;
	private ListView lv;
	private EditText editText_postComment;
	private TextView lbl_venueName;
	private RatingBar overallRatingBar, tonightsRatingBar;
	private ImageButton btn_infoCLub,btn_openVenueOnMap;
	private Intent intent;
	private InteractWithVenueController controller;
	private Comment comment;
	private Toast toast;
	private Handler handler;
	private int clubberID, venueID;
	private float overallRanking;
	private String venueName, venueDescription, venueAddress,
			venueOpeningHours, venueClosingHours;
	private boolean getLatestComments = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_interact_with_venue);
		handler = new Handler();
		intent = getIntent();
		controller = new InteractWithVenueController();
		// get widgets
		lv = (ListView) findViewById(R.id.listView_comments);
		btn_infoCLub = (ImageButton) findViewById(R.id.btn_clubInfo);
		btn_openVenueOnMap=(ImageButton) findViewById(R.id.btn_googleMaps);
		editText_postComment = (EditText) findViewById(R.id.editText_postComent);
		lbl_venueName = (TextView) findViewById(R.id.lbl_clubName);
		overallRatingBar = (RatingBar) findViewById(R.id.ratingBar_overallRanking);
		tonightsRatingBar = (RatingBar) findViewById(R.id.ratingBar_tonightRanking);
		// set adapters
		commentsListAdapter = new IwvCommentsListAdapter(
				getApplicationContext(), R.layout.listitem_comments);

		lv.setAdapter(commentsListAdapter);

	}

	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);


    }

	@SuppressWarnings("unchecked")
	@Override
	protected void onStart() {
		super.onStart();

		if (intent.getExtras() == null) {

			toast = Toast.makeText(InteractWithVenueActivity.this,
					"No data to show Activity InteractWithVenue finished",
					Toast.LENGTH_LONG);
			toast.show();
			finish();// finishes activity,nothing to show
		} else {

			// getting data
			clubberID = intent.getIntExtra("clubberID", -1);
			venueID = intent.getIntExtra("venueID", -1);
			overallRanking = intent.getFloatExtra("overallRanking", -1);
			venueName = intent.getStringExtra("venueName");
			venueDescription = intent.getStringExtra("venueDescription");
			venueAddress = intent.getStringExtra("venueAddress");
			venueOpeningHours = intent.getStringExtra("venueOpeningHours");
			venueClosingHours = intent.getStringExtra("venueClosingHours");

			// present data
			lbl_venueName.setText(venueName);
			overallRatingBar.setRating(overallRanking);

			commentsListAdapter.addAll((ArrayList<Comment>) intent
					.getSerializableExtra("commentList"), clubberID);

			handler.post(new Runnable() {

				@Override
				public void run() {

					if (getLatestComments) {
						commentsListAdapter.update(controller
								.getLatestComments(venueID, clubberID));
						commentsListAdapter.refresh();
						lv.smoothScrollToPosition(commentsListAdapter.getCount());

						handler.postDelayed(this, 1000); // set time to refresh
					}

				}

			});

			addListeners();
		}
	}

	private void addListeners() {

		editText_postComment.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {

				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {

					if (editText_postComment.getText().toString().trim()
							.length() > 0) {
						comment = new Comment(editText_postComment.getText()
								.toString(), new Timestamp(System
								.currentTimeMillis()), venueID, clubberID);
						// Perform action on key press
						commentsListAdapter.add(comment, clubberID);

						controller.addComment(comment);
						lv.smoothScrollToPosition(commentsListAdapter
								.getCount());

						editText_postComment.setText("");
					}
					return true;
				}
				return false;
			}
		});

		btn_infoCLub.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				toast = Toast.makeText(InteractWithVenueActivity.this,
						"Description: " + venueDescription + "\n" + "Address: "
								+ venueAddress + "\n" + "Opening Time: "
								+ venueOpeningHours + "\n" + "Closing Time: "
								+ venueClosingHours, Toast.LENGTH_LONG);
				toast.setGravity(Gravity.TOP, -30, 50);
				toast.show();

			}

		});
		
		btn_openVenueOnMap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//controller.openViewVenueOnMap(getApplicationContext(), venueID, clubberID);
			}
		});
		
		

		tonightsRatingBar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {

						controller.setTonightRate(new Rate(clubberID, venueID,
								tonightsRatingBar.getRating()));
					}
				});

	}

}
